/**
 * Created by lwy on 2017/6/23.
 */

//发送验证码
$('.code-button').click(function(){
    sendCode();
})
function sendCode(){

    if(!$('#phone').val().match(/^1[2-9][0-9]{9}$/)){
        mui.toast('请输入正确手机号码');
        return;
    }
    if(!ifCyber()){
        mui.toast('请检查网络');
    }

    //验证该手机号是否发送过验证码
    $.ajax({
        url:'/imageVerifyC/ifCode.json',
        dataType:'json',
        data:{
            mobile:$('#phone').val(),
        },
        type:'post',
        success:function (data) {
            if(data.ifCode){
                //如果为true，从本地获取图片验证码
                verifyCode = getItem('verifyCode');
                verify();
            }else{
                //如果为false，需要输入
                var timestamp = (new Date()).valueOf();
                $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val()+"&t="+timestamp);
                $('#code_input').val('');
                $('.zhezhao').show();
            }
        }
    })

}

document.getElementById('code_input').oninput=function () {
    if($(this).val().length==5){
        $.ajax({
            //验证码是否正确接口
            url:'/imageVerifyC/verifyCode.json',
            dataType:'json',
            data:{
                mobile:$('#phone').val(),
                validateCode:$('#code_input').val(),
            },
            type:'post',
            success:function (data) {
                if(data.code!=1){
                    mui.toast(data.msg);
                    return;
                }
                if(data.code==1){
                    if(data.verifyCode){
                        $('.zhezhao').hide();
                        twice=true;
                        verifyCode = $('#code_input').val();
                        setItem('verifyCode',$('#code_input').val());
                        verify();
                    }else{
                        mui.toast('验证码错误');
                        var timestamp = (new Date()).valueOf();
                        $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val()+"&t="+timestamp);
                        return;
                    }
                }
            }
        })
    }
}

//关闭验证窗口
$('#close').click(function () {
    $('.zhezhao').hide();
})

function verify() {
    $(".code-button").html("发送中…");
    $("#codeloginbox").unbind();
    $.ajax({
        url:'/user/sendMobileCode_ValidateCode.json',
        data:{
            mobile:$('#phone').val(),
            platform:'wap',
            validateCode:verifyCode,

        },
        dataType:'json',
        success:function(data) {
            if(data.code!=1){
                if(data.msg=='图形验证码验证失败'){
                    $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val());
                    $('#code_input').val('');
                    $('.zhezhao').show();
                    return;
                }
                mui.toast(data.msg);
                $(".code-button").html("重新发送");
                $("#codeloginbox").bind('click',sendCode);
                return;
            }
            timeUpdate.init($(".code-button"), 60);

        }
    })
}

var timeUpdate = {
    node:null,//节点
    time:60,//时间
    //主函数
    start:function(){
        if(this.time > 0){
            $(this.node).html( this.time-- + 's后重发');
            var _this = this;
            setTimeout(function(){
                _this.start();
            },1000);
        }else{
            this.node.click(function () {
                sendCode();
            });
            this.node.html("重新发送");
        }
    },
    //初始化
    init:function(node, timeLimit){
        this.node = node;
        this.time = timeLimit;
        this.start();
    }
};


//确定
$('.login-button').click(function(){
    var userName = $('#phone').val();
    var passWord = $('#password').val();
    if(passWord==""){
        mui.toast('密码不能为空');
        return;
    }
                $.ajax({
                    url:'/user/userLoginKey.json',
                    dataType:'json',
                    data:{
                        name:userName,
                    },
                    success:function(data){
                        if(data.code!=1){
                            mui.toast(data.msg);
                            return;
                        }
                        if(data.code==1){
                            passWord= calcMD5(passWord + data.datas.userKey, 32);
                            $.ajax({
                                url:'/user/findPassword.json',
                                dataType:'json',
                                type:'post',
                                data:{
                                    phone:userName,
                                    password:passWord,
                                    mobile_code:$('.code-input').val(),
                                    platform:'wap',
                                },
                                success:function(data){
                                    if(data.code!=1){
                                        mui.toast(data.msg);
                                        return;
                                    }
                                    if(data.code==1){
                                        removeItem('token');
                                        window.location.href="login.html";
                                    }
                                }
                            })
                        }
                    }
                })
})

